<?php
require_once 'Zend/File/Transfer/Adapter/Http.php';
class ProductsController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }
	
    public function addAction(){
    	//copy ("temp/images/avatar.jpg" , "images/aodai/avatar.jpg");
    }
    public  function createAction(){
    	if ($this->_request->isPost()){
    		//get param
    		$name_product= $_POST['txt_name'];
    		$description= $_POST['txt_description'];
    		$image= $_POST['hdf_image'];
    		$file_name = basename($image);
    		$db = Zend_Registry::get('db');
    		$log = Zend_Registry::get('log');
    		
    		$folder = "images/aodai/";
    		$new_path_image  = $folder.$file_name;
    		
	    	
	    	$log -> info ("file name".$file_name);
	    	$log -> info ("new_path_image".$new_path_image);
	    	
	    	$log -> info ("action create ");
	    	$db -> insert('products',array('name'=>$name_product,'description'=>$description,'image'=>$new_path_image));
	    	
	    	//get id
	    	$id = $db ->lastInsertId();
	    	echo "id: ".$id;
	    	$log -> info("move image from temp/images/$file_name to $new_path_image");
			copy ("temp/images/$file_name" , $new_path_image);
			unlink("/temp/images/$file_name");
			$log->info(" move uploaded file ".$move."d");
			//$this->_redirect("/products/list/pid/$id");
			$this->_redirect("/products/list");
			
    	}
    }
    public function listAction(){
    	$db = Zend_Registry::get('db');
    	echo "1";
    	$id = $this->_getParam('pid');
    	
    	echo "2";
    	$query ="select * from products";
    	echo $query;
    	
    	//fetch all data
    	$result = $db-> fetchAll($query);
    	
    	//set view
    	$this->view->products = $result;
    	
    }
	public function viewAction(){
    	$db = Zend_Registry::get('db');
    	echo "1";
    	$id = $this->_getParam('pid');
    	
    	echo "2";
    	$query ="select * from products where id = $id";
    	echo $query;
    	
    	//fetch all data
    	$result = $db-> fetchAll($query);
    	
    	//set view
    	$this->view->products = $result;
    	
    }

}

